package com.danico.danicoproject.skills;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.danico.danicoproject.DanicoProject;
import com.danico.danicoproject.tweenaccessors.WeaponAccessor;

public class Weapon {
	
	private TweenManager tweenManager;
	private int damage;
	public int getDamage(){return damage;}
	private Vector2 position;
	public Vector2 getPosition(){return position;}
	public Rectangle getHitbox(){
		if(hitting)
			return sprite.getBoundingRectangle();
		else
			return new Rectangle(0,0,0,0);
		}
	private Sprite sprite;
	public Sprite getSprite() { return sprite; }
	public void setPosition(float x, float y){position = new Vector2(x,y);}
	private boolean hitting, drawAtRight;
	public boolean getDrawAtRight() { return drawAtRight; }
	public void setDrawAtRight(boolean b){drawAtRight = b;}
	public void setHitting(boolean b){hitting = b;}
//	private float timer = 0;
	
	private final TweenCallback EndCallBack;
	
	
	public Weapon(int damage){
			
		this.damage = damage;
		sprite = new Sprite(DanicoProject.assets.get("data/sword.png", Texture.class));
		
		//Initialisation de la partie TweenEngine
		tweenManager = new TweenManager();
		Tween.registerAccessor(Weapon.class, new WeaponAccessor());
		EndCallBack = new TweenCallback() {
			@Override
			public void onEvent(int eventType, BaseTween<?> arg1) {			
					sprite.setRotation(0);
					hitting = false;
			}
			};
	}
	
	public void render(SpriteBatch batch, float delta, float x, float y){
		
		//GESTION DE LORIENTATION DU PERSO
		if(!drawAtRight){
			position = new Vector2(x+sprite.getWidth()+10, y+10);
			sprite.setOrigin(0, 0);
		}
		else if(drawAtRight){
			
			sprite.flip(true, false);
			sprite.setOrigin(sprite.getWidth(), 0);
			position = new Vector2(x+32-sprite.getWidth()-2, y+10);
		}
		sprite.setPosition(position.x,position.y);
		
		
		if (hitting){
			//TIMER ANIMATION
//			if (timer <=10){
//				
//				sprite.setRotation(0);
//			}
//			else if (timer <=20){
//				
//				if(!drawAtRight)
//					sprite.setRotation(45);
//				else
//					sprite.setRotation(315);
//			}
//			else if (timer<=30){
//				
//				if(!drawAtRight)
//					sprite.setRotation(90);
//				else
//					sprite.setRotation(270);
//			
//			
//			
//			}
//			
//			
//			
//			else{
//				hitting=false;
//			}
//			timer+=delta;
			
			if(!drawAtRight){
					Tween.to(this, WeaponAccessor.ROTATION, 0.3f).start(tweenManager)
					.target(350)
					.setCallbackTriggers(TweenCallback.COMPLETE)
					.setCallback(EndCallBack);				
			}
			else{
					Tween.to(this, WeaponAccessor.ROTATION, 0.3f).start(tweenManager)
					.target(-350)
					.setCallbackTriggers(TweenCallback.COMPLETE)
					.setCallback(EndCallBack);		
			}
			
			sprite.draw(batch);

			
		}
		//NE TAPE PAS
		else{
		//	timer=0;
		}
		
		if(drawAtRight)
			sprite.flip(true, false);
		
		tweenManager.update(Gdx.graphics.getDeltaTime());
	}

}